#!/bin/sh

_pkg_installed() {
    echo $(($(xbps-query -l | grep $1 | wc -l)))
}

# home (sweet home)
home_target_dir=/home/strlst
cd "$home_target_dir"
ln -sf ~/dotfiles"$home_target_dir"/.aliases ~/.aliases
echo "ln -sf ~/dotfiles$home_target_dir/.aliases ~/.aliases"
ln -sf ~/dotfiles"$home_target_dir"/.xinitrc ~/.xinitrc
echo "ln -sf ~/dotfiles$home_target_dir/.xinitrc ~/.xinitrc"
ln -sf ~/dotfiles"$home_target_dir"/.xprofile ~/.xprofile
echo "ln -sf ~/dotfiles$home_target_dir/.xprofile ~/.xprofile"
ln -sf ~/dotfiles"$home_target_dir"/.profile ~/.profile
echo "ln -sf ~/dotfiles$home_target_dir/.profile ~/.profile"
#ln -sf ~/dotfiles"$home_target_dir"/.mkshrc ~/.mkshrc
#echo "ln -sf ~/dotfiles$home_target_dir/.mkshrc ~/.mkshrc"
ln -sf ~/dotfiles"$home_target_dir"/.bashrc ~/.bashrc
echo "ln -sf ~/dotfiles$home_target_dir/.mkshrc ~/.mkshrc"
ln -sf ~/dotfiles"$home_target_dir"/.bash_profile ~/.bash_profile
echo "ln -sf ~/dotfiles$home_target_dir/.mkshrc ~/.mkshrc"
ln -sf ~/dotfiles"$home_target_dir"/.urlview ~/.urlview
echo "ln -sf ~/dotfiles$home_target_dir/.urlview ~/.urlview"
ln -sf ~/dotfiles"$home_target_dir"/.xbindkeysrc ~/.xbindkeysrc 
echo "ln -sf ~/dotfiles$home_target_dir/.xbindkeysrc ~/.xbindkeysrc"

# xq -l:
#  your pkg manager here, where the query returns 1 line or none
# grep newsboat
#  your package for newsboat here, depending which name pkg sources use
if [ $(_pkg_installed newsboat) -eq 1 ]; then
    ln -sf ~/dotfiles"$home_target_dir"/.newsboat/config ~/.newsboat/config
    echo "ln -sf ~/dotfiles$home_target_dir/.newsboat/config ~/.newsboat/config"
    ln -sf ~/dotfiles"$home_target_dir"/.newsboat/urls ~/.newsboat/urls
    echo "ln -sf ~/dotfiles$home_target_dir/.newsboat/urls ~/.newsboat/urls"
fi

if [ $(_pkg_installed ghc) -eq 1 ]; then
    ln -sf ~/dotfiles"$home_target_dir"/.ghci ~/.ghci
    echo "ln -sf ~/dotfiles$home_target_dir/.ghci ~/.ghci"
fi

if [ $(_pkg_installed qutebrowser) -eq 1 ]; then
    cp -r ~/dotfiles"$home_target_dir"/.config/qutebrowser ~/.config
    echo "cp -r ~/dotfiles$home_target_dir/.config/qutebrowser ~/.config"
    cp -r ~/dotfiles"$home_target_dir"/.local/share/qutebrowser ~/.local/share
    echo "cp -r ~/dotfiles$home_target_dir/.local/share/qutebrowser ~/.local/share"
fi

[ ! -d ./bin ] &
    cp -r ~/dotfiles"$home_target_dir"/bin .
echo "cp -r ~/dotfiles$home_target_dir/bin ."
[ ! -d ./.config ] &
    cp -r ~/dotfiles"$home_target_dir"/.config .
echo "cp -r ~/dotfiles$home_target_dir/.config ."

# bgs
rm -rf ~/.config/walls
echo "rm -rf ~/.config/walls"
cp -r ~/dotfiles"$home_target_dir"/.config/walls ~/.config/
echo "cp -r ~/dotfiles$home_target_dir/.config/walls ~/.config/"

# vis
vis_target_dir=/usr/share/vis
sudo cp ~/dotfiles"$vis_target_dir"/visrc.lua "$vis_target_dir"
echo "sudo cp ~/dotfiles$vis_target_dir/visrc.lua $vis_target_dir"
sudo cp -r ~/dotfiles"$vis_target_dir"/themes "$vis_target_dir"
echo "sudo cp -r ~/dotfiles$vis_target_dir/themes $vis_target_dir"
sudo cp -r ~/dotfiles"$vis_target_dir"/plugins "$vis_target_dir"
echo "sudo cp -r ~/dotfiles$vis_target_dir/plugins $vis_target_dir"

# lf
cp -r ~/dotfiles"$home_target_dir"/.config/lf "$home_target_dir"/.config
echo "cp -r ~/dotfiles$home_target_dir/.config/lf $home_target_dir/.config"

# mnt
mkdir -p ~/1mnt ~/2mnt ~/3mnt
echo "mkdir -p ~/1mnt ~/2mnt ~/3mnt"

# backlight
[ $PERSONALDEVICE -eq 1 ] &&
    sudo sudo cp ~/dotfiles"$home_target_dir"/.config/udev/20-backlight.rules /usr/lib/udev/rules.d/20-backlight.rules &&
    echo "sudo cp ~/dotfiles$home_target_dir/.config/udev/20-backlight.rules /usr/lib/udev/rules.d/20-backlight.rules"
